from fastapi import FastAPI, Depends
from sqlmodel import Session, select
from sqlmodel.ext.asyncio.session import AsyncSession
from app.db import get_session, init_db

from app.models import Song, SongCreate

app = FastAPI()


# 如果要在初始的时候执行某些动作，可以使用startup事件
# @app.on_event("startup")
async def on_startup():
    await init_db()


@app.get("/ping")
async def pong():
    return {"ping": "pong!"}


@app.get("/songs", response_model=list[Song])
async def get_songs(session: AsyncSession = Depends(get_session)):
    result = await session.exec(select(Song))
    # songs = result.scalars().all()
    songs = result.all()
    return [Song(name=song.name, artist=song.artist, year=song.year, id=song.id) for song in songs]


@app.post("/songs")
async def add_song(song: SongCreate, session: AsyncSession = Depends(get_session)):
    song = Song(name=song.name, artist=song.artist, year=song.year)
    session.add(song)
    await session.commit()
    await session.refresh(song)
    return song
